package cn.fl.arbitration.web.apply.controller;

import cmm.arbitration.facade.arbitration.api.IArbitrationApiService;
import cmm.arbitration.facade.arbitration.api.ITcArbFileApiService;
import cmm.arbitration.facade.arbitration.condition.TcArbApplyQC;
import cmm.arbitration.facade.arbitration.condition.TcArbFileApplyQC;
import cmm.arbitration.facade.arbitration.dto.TcArbApplyDTO;
import cmm.arbitration.facade.arbitration.dto.TcArbClaimDTO;
import cmm.arbitration.facade.arbitration.dto.TcArbFileApplyDTO;
import cmm.arbitration.facade.arbitration.dto.TcArbLitigantDTO;
import cmm.arbitration.facade.arbitration.dto.req.*;
import cmm.arbitration.facade.arbitration.dto.resp.ApiArbitrationInitiateRespDTO;
import cmm.arbitration.facade.arbitration.enm.ApiFileTypeEnum;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.arbitration.utils.PropKit;
import cn.fl.arbitration.utils.ValidateUtils;
import cn.fl.arbitration.web.apply.service.IFileUploadService;
import cn.fl.arbitration.web.apply.service.ITcArbApplyService;
import cn.fl.arbitration.web.apply.transform.TcArbApplyVOTransform;
import cn.fl.arbitration.web.apply.vo.ApplyInfoVO;
import cn.fl.arbitration.web.apply.vo.TcArbApplyFileResp;
import cn.fl.arbitration.web.apply.vo.TcArbApplyVO;
import cn.fl.arbitration.web.apply.vo.UpLoadFileRespVo;
import cn.fl.arbitration.web.apply.vo.resp.FileUploadRespVo;
import cn.fl.system.utils.FileTypeUtil;
import cn.lionbridgecapital.loan.model.dto.McrExternalDTO;
import cn.lionbridgecapital.loan.service.api.IMcrExternalApiService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;
import cn.fl.framework.base.context.CurrentThreadContext;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Description: 仲裁申请信息 控制层
 */
@Controller
@RequestMapping("/arbitration/tcarbapply")
public class TcArbApplyController extends BaseController {
	private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TcArbApplyController.class);


	@Resource
	private IFileUploadService fileUploadService;
	@Resource
	private ITcArbApplyService tcArbApplyService;
	@Resource
	private IArbitrationApiService arbitrationApiService;

	@Resource
	private IMcrExternalApiService mcrExternalApiService;

	@Resource
	private ITcArbFileApiService tcArbFileApiService;
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		ModelAndView view = new ModelAndView("arbitration/tcArbApply/tcArbApplyList");
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return view;

	}
	@RequestMapping("/upload.do")
	public @ResponseBody
	FileUploadRespVo uploadImage(Long applyId, String templateCode, HttpServletRequest request) {

		TcArbApplyDTO tcArbApplyDTO = tcArbApplyService.selectById(applyId);
		if(tcArbApplyDTO==null){
			 	return new FileUploadRespVo("",0,"","没有找到仲裁申请记录");
		}
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
		if(multipartResolver.isMultipart(request)) {
			//转换成多部分request
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			try {
				Iterator<String> iter = multipartRequest.getFileNames();
				while (iter.hasNext()) {
					List<MultipartFile> fileList = multipartRequest.getFiles(iter.next());
					for (MultipartFile f : fileList) {
						Map<String, String> map = FileTypeUtil.judgeFileType(f);
						if (!"200".equals(map.get("code"))) {
							return new FileUploadRespVo("",0,"",map.get("info"));
						}
					}
				}
				UpLoadFileRespVo upload = fileUploadService.upload("cn/fl/arbitration", multipartRequest);
				if(upload==null){
					return new FileUploadRespVo("",0,"","上传文件服务器失败");
				}else{

					ApiArbitrationReqDTO<ApiArbitrationFileAddReqDTO> apiArbitrationReqDTO = new ApiArbitrationReqDTO<>();
					ApiArbitrationReqHeadDTO apiArbitrationReqHeadDTO = new ApiArbitrationReqHeadDTO();
					apiArbitrationReqHeadDTO.setpId("GHSKKL8IEADS1");
					apiArbitrationReqHeadDTO.setbId("b4b76a4cbc7d4f10b70353b3ab0e8e2f");
					apiArbitrationReqDTO.setHead(apiArbitrationReqHeadDTO);

					ApiArbitrationFileAddReqDTO apiArbitrationFileAddReqDTO = new ApiArbitrationFileAddReqDTO();
					apiArbitrationFileAddReqDTO.setFileTemplateId(templateCode);
					apiArbitrationFileAddReqDTO.setBusinessId(tcArbApplyDTO.getBusinessId());
					apiArbitrationFileAddReqDTO.setApiFileTypeEnum(ApiFileTypeEnum.PATH_ABS);
					apiArbitrationFileAddReqDTO.setFileValue(upload.getAbsPath());
					apiArbitrationFileAddReqDTO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
					apiArbitrationFileAddReqDTO.setUptUsrId(CurrentThreadContext.getCurrentUserId());
					apiArbitrationReqDTO.setBody(apiArbitrationFileAddReqDTO);
					DataResultDTO apply = arbitrationApiService.arbitrationFileAdd(apiArbitrationReqDTO);
					if(apply.getSuccess()==1){
						ApiArbitrationReqDTO<ApiArbitrationNoticeUploadDTO> reqDTOApiArbitrationReqDTO = new ApiArbitrationReqDTO<>();
						ApiArbitrationNoticeUploadDTO apiArbitrationNoticeUploadDTO = new ApiArbitrationNoticeUploadDTO();
						apiArbitrationNoticeUploadDTO.setBusinessId(tcArbApplyDTO.getBusinessId());
						/*五矿 :
云信: */
						if(templateCode.indexOf("_YX_")!=-1){
							apiArbitrationNoticeUploadDTO.setProductType("XD_PRO_YX");
						}else{
							apiArbitrationNoticeUploadDTO.setProductType("XD_PRO_WK");
						}

						reqDTOApiArbitrationReqDTO.setHead(apiArbitrationReqHeadDTO);
						reqDTOApiArbitrationReqDTO.setBody(apiArbitrationNoticeUploadDTO);
					  arbitrationApiService.noticeUpload(reqDTOApiArbitrationReqDTO);

						return new FileUploadRespVo(upload.getName(),1,upload.getAbsPath());
					}else{
						FileUploadRespVo fileUploadRespVo = new FileUploadRespVo("", 0, "");
						fileUploadRespVo.setDes(apply.getInfo());
						return fileUploadRespVo;
					}

				}
			} catch (Exception e) {
				BaseLogUtils.newLogger(e.getMessage()).error(LogExceptionEnum.API_CODE_0101,e);
				return new FileUploadRespVo("",0,"",e.getMessage());
			}

		}else{
			return new FileUploadRespVo("",0,"","请上传附件");
		}

	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/applyContract.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object applyContract(Long  applyId) throws BaseException {
		TcArbApplyDTO tcArbApplyDTO = tcArbApplyService.selectById(applyId);
		if(tcArbApplyDTO==null){
			return new DataResultVO(BaseConstant.IS_YESNO_NO, "未找到申请信息!");
		}
		if(tcArbApplyDTO.getStatus().equals("1")){
			ApiArbitrationReqDTO<ApiArbitrationContractCreatReqDTO> apiArbitrationReqDTO = new ApiArbitrationReqDTO<>();
			ApiArbitrationReqHeadDTO apiArbitrationReqHeadDTO = new ApiArbitrationReqHeadDTO();
			apiArbitrationReqHeadDTO.setpId("GHSKKL8IEADS1");
			apiArbitrationReqHeadDTO.setbId("b4b76a4cbc7d4f10b70353b3ab0e8e2f");
			apiArbitrationReqDTO.setHead(apiArbitrationReqHeadDTO);
			ApiArbitrationContractCreatReqDTO apiArbitrationContractCreatReqDTO = new ApiArbitrationContractCreatReqDTO();
			apiArbitrationContractCreatReqDTO.setApplyId(tcArbApplyDTO.getId());
			apiArbitrationReqDTO.setBody(apiArbitrationContractCreatReqDTO);
			DataResultDTO dataResultDTO = arbitrationApiService.initiateArbitrationContract(apiArbitrationReqDTO);
			  return new DataResultVO(dataResultDTO.getSuccess(), dataResultDTO.getInfo());
		}else{
			if(tcArbApplyDTO.getStatus().equals("0")){
				return new DataResultVO(BaseConstant.IS_YESNO_NO, "还没提交仲裁");
			}else{
				return new DataResultVO(BaseConstant.IS_YESNO_NO, "已上传电子合同");
			}

		}

	}
	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/getFile.do", method = { RequestMethod.GET})
	public @ResponseBody DataResultVO getFile(Long applyId,String templateCode) {



		TcArbApplyDTO tcArbApplyDTO = tcArbApplyService.selectById(applyId);
		if(tcArbApplyDTO==null){
			return  new DataResultVO(BaseConstant.IS_YESNO_NO, "");
		}
		String businessId = tcArbApplyDTO.getBusinessId();
		TcArbFileApplyQC tcArbFileApplyQC = new TcArbFileApplyQC();
		tcArbFileApplyQC.setIsDel(0);
		tcArbFileApplyQC.setIsValid(1);
		tcArbFileApplyQC.setBusinessId(businessId);
		tcArbFileApplyQC.setCustomTemplateCode(templateCode);
		java.util.List<TcArbFileApplyDTO> tcArbFileApplyDTOS = tcArbFileApiService.searchList(tcArbFileApplyQC);
		java.util.List<TcArbApplyFileResp> list = Lists.newArrayList();
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "成功!");
		if(CollectionUtils.isNotEmpty(tcArbFileApplyDTOS)){

			for (TcArbFileApplyDTO tcArbFileApplyDTO : tcArbFileApplyDTOS) {
				TcArbApplyFileResp tcArbApplyFileResp = new TcArbApplyFileResp();
				BeanUtils.copyProperties(tcArbFileApplyDTO,tcArbApplyFileResp);
				list.add(tcArbApplyFileResp);
			}
			result.setData(list);
			return result;
		}else{
			return  new DataResultVO(BaseConstant.IS_YESNO_YES, "成功!");
		}
	}
	
	/**
	 * 到新增修改页面
	 */
	@RequestMapping(value = "/applyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toAddPage", "加载新增.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		ModelAndView view = new ModelAndView("arbitration/tcArbApply/tcArbApplyAdd");
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toAddPage", "加载新增.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}
	/**
	 * 到新增修改页面
	 */
	@RequestMapping(value = "/detailPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView detailPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toAddPage", "加载新增.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

		ModelAndView view = new ModelAndView("arbitration/tcArbApply/tcArbApplyDetail");

		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toAddPage", "加载新增.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}


	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "initList", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String certNo = request.getParameter("certNo");
		String businessId = request.getParameter("businessId");
		String status = request.getParameter("status");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		TcArbApplyQC qc = new TcArbApplyQC();
		if (StringUtils.isNotEmpty(certNo)) {
			qc.setCertNo(certNo);
		}
		if (StringUtils.isNotEmpty(businessId)) {
			qc.setBusinessId(businessId);
		}
		if (StringUtils.isNotEmpty(status)) {
			qc.setStatus(status);
		}
		page.setCondition(qc);

		Pager pager =  this.tcArbApplyService.searchListPage(page);
		java.util.List<TcArbApplyVO> list = TcArbApplyVOTransform.toVOList((List<TcArbApplyDTO>) pager.getResultList());

		for (TcArbApplyVO tcArbApplyVO : list) {
			try {DataResultDTO arbitrationPersonInfo = mcrExternalApiService.getArbitrationPersonInfo(tcArbApplyVO.getBusinessId());
			if(arbitrationPersonInfo.getSuccess()==1){

 			McrExternalDTO data = (McrExternalDTO) arbitrationPersonInfo.getData();
				tcArbApplyVO.setApplyStatus(tcArbApplyVO.getStatus());
				tcArbApplyVO.setCompanyNm(data.getLoanChannel());
				tcArbApplyVO.setUsrNm(data.getPersonName());
				tcArbApplyVO.setOverdueAmount(data.getOverTotalAmt()+"");
				tcArbApplyVO.setOverdueDate(DateUtils.formatDateToString(data.getOverdueDate(),DateUtils.YYYY_MM_DD));
				tcArbApplyVO.setOverdueTimes(data.getOverduePd()+"");
				tcArbApplyVO.setPhoneNo(data.getMobile());
				tcArbApplyVO.setPrincipal(data.getCrAmt()+"");
				tcArbApplyVO.setRepaymentDate(DateUtils.formatDateToString(data.getLoanEndDate(),DateUtils.YYYY_MM_DD));
				BaseLogUtils.newLogger(JSONObject.toJSONString(data)).info();
			}
			}catch (Exception e){
				BaseLogUtils.newLogger(e.getMessage()).error(LogExceptionEnum.API_CODE_0102,e);
			}

		}
		ListPageVO<TcArbApplyVO> listPage = new ListPageVO<TcArbApplyVO>(list, pager.getTotalCount());
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "initList", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}
	
	/**
	 * 新增
	 */
	@RequestMapping(value = "/apply.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(ApplyInfoVO applyInfoVO) throws BaseException {
		DataResultVO validate = ValidateUtils.validate(applyInfoVO);
		if(validate.getSuccess()==1){
		}else{
			return validate;
		}
		ApiArbitrationInitiateReqDTO apiArbitrationInitiateReqDTO = new ApiArbitrationInitiateReqDTO();
		apiArbitrationInitiateReqDTO.setApplyId(applyInfoVO.getApplyId());
		apiArbitrationInitiateReqDTO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
		apiArbitrationInitiateReqDTO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
		ApiArbitrationReqDTO<ApiArbitrationInitiateReqDTO> apiArbitrationReqDTO = new ApiArbitrationReqDTO<>();
		ApiArbitrationReqHeadDTO apiArbitrationReqHeadDTO = new ApiArbitrationReqHeadDTO();
		apiArbitrationReqHeadDTO.setpId("GHSKKL8IEADS1");
		apiArbitrationReqHeadDTO.setbId("b4b76a4cbc7d4f10b70353b3ab0e8e2f");
		apiArbitrationReqDTO.setHead(apiArbitrationReqHeadDTO);
		apiArbitrationReqDTO.setBody(apiArbitrationInitiateReqDTO);
		ApiArbitrationClaimReqDTO apiArbitrationClaimReqDTO = new ApiArbitrationClaimReqDTO();
		apiArbitrationClaimReqDTO.setClientId(applyInfoVO.getClientId());
		apiArbitrationClaimReqDTO.setProductId(applyInfoVO.getProductId());
		apiArbitrationClaimReqDTO.setContractNumber(applyInfoVO.getContractCode());
		apiArbitrationClaimReqDTO.setConclusionData(applyInfoVO.getConclusionData());
		apiArbitrationClaimReqDTO.setReason(applyInfoVO.getReason());
		apiArbitrationClaimReqDTO.setClaimContent(applyInfoVO.getClaimContent());
	//	apiArbitrationClaimReqDTO.setDisputesType(applyInfoVO.getD);
		apiArbitrationClaimReqDTO.setMediationType(applyInfoVO.getMediationType());
		apiArbitrationClaimReqDTO.setLoanAmount(applyInfoVO.getLoanAmount());
		apiArbitrationClaimReqDTO.setDisputeFee(applyInfoVO.getDisputeFee());
		apiArbitrationClaimReqDTO.setInterestType(applyInfoVO.getInterestType());
		apiArbitrationClaimReqDTO.setInterestRate(applyInfoVO.getInterestRate());
		apiArbitrationClaimReqDTO.setBorrowingStartDate(applyInfoVO.getBorrowingStartDate());
		apiArbitrationClaimReqDTO.setBorrowingEndDate(applyInfoVO.getBorrowingEndDate());
		apiArbitrationClaimReqDTO.setBorrowingNum(applyInfoVO.getBorrowingNum());
		apiArbitrationClaimReqDTO.setReturnType(applyInfoVO.getReturnType());
		apiArbitrationClaimReqDTO.setPaymentAccount(applyInfoVO.getPaymentAccount());
		apiArbitrationClaimReqDTO.setPaymentDate(applyInfoVO.getPaymentDate());
		apiArbitrationClaimReqDTO.setPaymentBank(applyInfoVO.getPaymentBank());
		apiArbitrationClaimReqDTO.setOverdueDate(applyInfoVO.getOverdueDate());
		apiArbitrationClaimReqDTO.setOverdueNum(applyInfoVO.getOverdueNum());
	//	apiArbitrationClaimReqDTO.setPenalty(applyInfoVO.getpe);
		//apiArbitrationClaimReqDTO.setAssignmentDate(applyInfoVO.getOverdueDate());
		//apiArbitrationClaimReqDTO.setExtraInfo();
		apiArbitrationInitiateReqDTO.setClaim(apiArbitrationClaimReqDTO);

		ApiArbitrationLitigantsReqDTO apiArbitrationLitigantsReqDTO = new ApiArbitrationLitigantsReqDTO();
		apiArbitrationLitigantsReqDTO.setLitigantType("1");
		apiArbitrationLitigantsReqDTO.setName(applyInfoVO.getName());
		apiArbitrationLitigantsReqDTO.setNature(applyInfoVO.getNature());
		apiArbitrationLitigantsReqDTO.setNation(applyInfoVO.getNation());
		apiArbitrationLitigantsReqDTO.setIdentityType(applyInfoVO.getIdentityType());
		apiArbitrationLitigantsReqDTO.setIdentityNumber(applyInfoVO.getIdentityNumber());
		//apiArbitrationLitigantsReqDTO.setLegalRepresent(applyInfoVO.getl);
		//apiArbitrationLitigantsReqDTO.setLegalPost(applyInfoVO);
		apiArbitrationLitigantsReqDTO.setLegalPhone(applyInfoVO.getPhoneNumber());
		//apiArbitrationLitigantsReqDTO.setLegalNumber();
		 apiArbitrationLitigantsReqDTO.setGender(applyInfoVO.getGender());
		apiArbitrationLitigantsReqDTO.setBirthday(applyInfoVO.getBirthday());
		apiArbitrationLitigantsReqDTO.setPhone(applyInfoVO.getPhoneNumber());
		//apiArbitrationLitigantsReqDTO.setEmail();
		apiArbitrationLitigantsReqDTO.setAddress(applyInfoVO.getAddress());
	//	apiArbitrationLitigantsReqDTO.setBankName(applyInfoVO.get);
		//apiArbitrationLitigantsReqDTO.setBankNumber();
		//apiArbitrationLitigantsReqDTO.setExtraInfo();
		ApiArbitrationLitigantsReqDTO apiArbitrationLitigantsReqDTO2 = new ApiArbitrationLitigantsReqDTO();
		apiArbitrationLitigantsReqDTO2.setLitigantType("0");
		apiArbitrationLitigantsReqDTO2.setName("天津卓远融资担保有限公司");
		apiArbitrationLitigantsReqDTO2.setNature("1");
		apiArbitrationLitigantsReqDTO2.setIdentityType("1");
		apiArbitrationLitigantsReqDTO2.setIdentityNumber("91120118MA06UN6668");
		apiArbitrationLitigantsReqDTO2.setLegalRepresent("万钧");
		apiArbitrationLitigantsReqDTO2.setLegalPost("执行董事");
		apiArbitrationLitigantsReqDTO2.setLegalPhone("15803119458");
		apiArbitrationLitigantsReqDTO2.setLegalNumber("510228197212264614");
		apiArbitrationLitigantsReqDTO2.setGender("0");
		apiArbitrationLitigantsReqDTO2.setNation("汉族");
		//apiArbitrationLitigantsReqDTO2.setBirthday(applyInfoVO.getBirthday());
		apiArbitrationLitigantsReqDTO2.setPhone("15803119458");
		//apiArbitrationLitigantsReqDTO.setEmail();
		apiArbitrationLitigantsReqDTO2.setAddress("天津自贸试验区（中心商务区）金昌道637号宝正大厦2805室");
		//	apiArbitrationLitigantsReqDTO.setBankName(applyInfoVO.get);
		//apiArbitrationLitigantsReqDTO.setBankNumber();
		//apiArbitrationLitigantsReqDTO.setExtraInfo();
		List<ApiArbitrationLitigantsReqDTO> litigantsReqDTOS = Lists.newArrayList();
		litigantsReqDTOS.add(apiArbitrationLitigantsReqDTO);
		litigantsReqDTOS.add(apiArbitrationLitigantsReqDTO2);
		apiArbitrationInitiateReqDTO.setLitigants(litigantsReqDTOS);
		DataResultDTO apply = arbitrationApiService.initiateArbitration(apiArbitrationReqDTO);

		BaseLogUtils.newLogger("返回结果,状态:{},提示信息:{}",apply.getSuccess(),apply.getInfo()).info();
		if(apply.getSuccess()==1){
			DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "新增成功!");
			return result;
		}else{
			DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_NO, apply.getInfo());
			return result;
		}

	}
	/**
	 * 新增
	 * @param id
	 */
	@RequestMapping(value = "/applyInfo.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object applyInfo(Long id) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_NO, "请求失败!");
		TcArbApplyDTO tcArbApplyDTO = tcArbApplyService.selectById(id);
		if(tcArbApplyDTO==null){
			return new DataResultVO(BaseConstant.IS_YESNO_NO, "未找到申请信息!");
		}else{
			ApplyInfoVO applyInfoVO = new ApplyInfoVO();

			ApiArbitrationReqDTO<ApiGetArbitrationInfoReqDTO> apiArbitrationReqDTO = new ApiArbitrationReqDTO<>();
			ApiArbitrationReqHeadDTO apiArbitrationReqHeadDTO = new ApiArbitrationReqHeadDTO();
			apiArbitrationReqHeadDTO.setpId("GHSKKL8IEADS1");
			apiArbitrationReqHeadDTO.setbId("b4b76a4cbc7d4f10b70353b3ab0e8e2f");
			apiArbitrationReqDTO.setHead(apiArbitrationReqHeadDTO);
			ApiGetArbitrationInfoReqDTO apiArbitrationInitiateReqDTO = new ApiGetArbitrationInfoReqDTO();
			apiArbitrationInitiateReqDTO.setApplyId(id);
			apiArbitrationReqDTO.setBody(apiArbitrationInitiateReqDTO);
			DataResultDTO apply = arbitrationApiService.getArbitrationInfo(apiArbitrationReqDTO);
			BaseLogUtils.newLogger(JSON.toJSONString(apply)).info();
			if(apply.getSuccess()==1){
				ApiArbitrationInitiateRespDTO data = (ApiArbitrationInitiateRespDTO) apply.getData();
				applyInfoVO.setClientId(data.getClientId());
				applyInfoVO.setProductId(data.getProductId());
				applyInfoVO.setApplyId(data.getApplyId());
				applyInfoVO.setXdContractCode(tcArbApplyDTO.getContractCode());
				applyInfoVO.setProductName(data.getProductName());
				applyInfoVO.setName(tcArbApplyDTO.getUsrNm());
				String businessId = tcArbApplyDTO.getBusinessId();
				TcArbFileApplyQC tcArbFileApplyQC = new TcArbFileApplyQC();
				tcArbFileApplyQC.setIsDel(0);
				tcArbFileApplyQC.setIsValid(1);
				tcArbFileApplyQC.setBusinessId(businessId);
				tcArbFileApplyQC.setIsMainCompact("1");
				List<TcArbFileApplyDTO> tcArbFileApplyDTOS = tcArbFileApiService.searchList(tcArbFileApplyQC);
				if(CollectionUtils.isNotEmpty(tcArbFileApplyDTOS)) {

					TcArbFileApplyDTO tcArbFileApplyDTO = tcArbFileApplyDTOS.get(0);
					applyInfoVO.setContractCode(tcArbFileApplyDTO.getMainCompactIdCode());
				}
				if(data.getClaim()!=null){
					TcArbClaimDTO claim = data.getClaim();
					applyInfoVO.setLoanAmount(claim.getLoanAmount());
					applyInfoVO.setDisputeFee(claim.getDisputeFee());
					applyInfoVO.setInterestType(claim.getInterestType());
					applyInfoVO.setInterestRate(claim.getInterestRate());
					applyInfoVO.setBorrowingStartDate(claim.getBorrowingStartDate());
					applyInfoVO.setBorrowingEndDate(claim.getBorrowingEndDate());
					applyInfoVO.setBorrowingNum(claim.getBorrowingNum());
					applyInfoVO.setReturnType(claim.getReturnType());
					applyInfoVO.setPaymentAccount(claim.getPaymentAccount());
					applyInfoVO.setPaymentBank(claim.getPaymentBank());
					applyInfoVO.setOverdueDate(claim.getOverdueDate());
					applyInfoVO.setOverdueNum(claim.getOverdueNum());
					//applyInfoVO.setContractCode(claim.getContractNumber());
					applyInfoVO.setConclusionData(claim.getConclusionData());
					applyInfoVO.setMediationType(claim.getMediationType());
					applyInfoVO.setReason(claim.getReason());
					applyInfoVO.setClaimContent(claim.getClaimContent());
				}

				if(data.getLitigants()!=null){
					List<TcArbLitigantDTO> litigants = data.getLitigants();
					TcArbLitigantDTO tcArbLitigantDTO = new TcArbLitigantDTO();
					for (TcArbLitigantDTO litigant : litigants) {
						if("1".equals(litigant.getLitigantType())){
							tcArbLitigantDTO= litigant;
						}
					}

					applyInfoVO.setName(tcArbLitigantDTO.getName());
					applyInfoVO.setLitigantType(tcArbLitigantDTO.getLitigantType());
					applyInfoVO.setNature(tcArbLitigantDTO.getNature());
					applyInfoVO.setIdentityType(tcArbLitigantDTO.getIdentityType());
					applyInfoVO.setIdentityNumber(tcArbLitigantDTO.getIdentityNumber());
					applyInfoVO.setGender(tcArbLitigantDTO.getGender());
					applyInfoVO.setBirthday(tcArbLitigantDTO.getBirthday());
					applyInfoVO.setNation(tcArbLitigantDTO.getNation());
					applyInfoVO.setPhoneNumber(tcArbLitigantDTO.getPhone());
					applyInfoVO.setAddress(tcArbLitigantDTO.getAddress());
				}
				try {
					DataResultDTO arbitrationPersonInfo = mcrExternalApiService.getArbitrationPersonInfo(data.getBusinessId());
					McrExternalDTO data1 = (McrExternalDTO) arbitrationPersonInfo.getData();
					if(StringUtils.isNotBlank(data1.getPersonName()))
					applyInfoVO.setName(data1.getPersonName());
					if(StringUtils.isNotBlank(data1.getCertTypeCd()))
					applyInfoVO.setIdentityType(data1.getCertTypeCd());
					if(StringUtils.isNotBlank(data1.getCertNo()))
					applyInfoVO.setIdentityNumber(data1.getCertNo());
					if(data1.getBirthday()!=null)
					applyInfoVO.setBirthday(DateUtils.formatDateToString(data1.getBirthday(),DateUtils.YYYY_MM_DD));
					if(StringUtils.isNotBlank(data1.getNation())){
						applyInfoVO.setNation(data1.getNation());
					}
					if(StringUtils.isNotBlank(data1.getAddress()))
					applyInfoVO.setAddress(data1.getAddress());
					if(data1.getOverduePrincipal()!=null)
					applyInfoVO.setLoanAmount(data1.getOverduePrincipal()+"");
					if(data1.getTargetPrincipal()!=null)
					applyInfoVO.setDisputeFee(data1.getTargetPrincipal()+"");
					if(StringUtils.isNotBlank(data1.getInterestType()))
					applyInfoVO.setInterestType(data1.getInterestType());
					if(data1.getInterestRate()!=null)
					applyInfoVO.setInterestRate(data1.getInterestRate()+"");
					if(data1.getLoanStartDate()!=null)
					applyInfoVO.setBorrowingStartDate(DateUtils.formatDateToString(data1.getLoanStartDate(),DateUtils.YYYY_MM_DD));
					if(data1.getLoanEndDate()!=null)
					applyInfoVO.setBorrowingEndDate(DateUtils.formatDateToString(data1.getLoanEndDate(),DateUtils.YYYY_MM_DD));
					if(data1.getPayPd()!=null)
				 	applyInfoVO.setBorrowingNum(data1.getPayPd()+"");
					if(StringUtils.isNotBlank(data1.getRepayType()))
					applyInfoVO.setReturnType(data1.getRepayType());
					if(StringUtils.isNotBlank(data1.getCapitalAccount()))
					applyInfoVO.setPaymentAccount(data1.getCapitalAccount());
					if(StringUtils.isNotBlank(data1.getCapitalBank()))
					applyInfoVO.setPaymentBank(data1.getCapitalBank());
					if(data1.getOverdueDate()!=null)
					applyInfoVO.setOverdueDate(DateUtils.formatDateToString(data1.getOverdueDate(),DateUtils.YYYY_MM_DD));
					if(data1.getOverduePd()!=null)
				 	applyInfoVO.setOverdueNum(data1.getOverduePd()+"");
				 //	applyInfoVO.setContractCode(data.getArbitrationContractCode());
					if(data1.getContSignDate()!=null)
			 		applyInfoVO.setConclusionData(DateUtils.formatDateToString(data1.getContSignDate(),DateUtils.YYYY_MM_DD));
					if(StringUtils.isNotBlank(data1.getRealAndReason()))
					applyInfoVO.setReason(data1.getRealAndReason());
					if(StringUtils.isNotBlank(data1.getArbitrationClaims()))
					applyInfoVO.setClaimContent(data1.getArbitrationClaims());
					if(StringUtils.isNotBlank(data1.getLoanChannel()))
					applyInfoVO.setXdCompany(data1.getLoanChannel());
					if(data1.getContSignDate()!=null)
					applyInfoVO.setPaymentDate(DateUtils.formatDateToString(data1.getContSignDate(),DateUtils.YYYY_MM_DD));
					if(StringUtils.isNotBlank(data1.getMobile()))
					applyInfoVO.setPhoneNumber(data1.getMobile());

				}catch (Exception e){
					BaseLogUtils.newLogger(e.getMessage()).error(LogExceptionEnum.API_CODE_0102,e);
				}

				DataResultVO dataResultVO = new DataResultVO(BaseConstant.IS_YESNO_YES, "请求成功!");
				dataResultVO.setData(applyInfoVO);
				return dataResultVO;
			}else{
				return result;
			}

		}
	}

	@RequestMapping(value = "/fileList.do", method = { RequestMethod.GET })
	@ResponseBody
	public Object add(String  applyId) throws BaseException {
		 //获取类型
		TcArbApplyDTO tcArbApplyDTO = tcArbApplyService.selectById(Long.valueOf(applyId));
		if(tcArbApplyDTO==null){
			return new DataResultVO(BaseConstant.IS_YESNO_NO, "未找到申请信息!");
		}else {
			TcArbFileApplyQC tcArbFileApplyQC = new TcArbFileApplyQC();
			tcArbFileApplyQC.setIsDel(0);
			tcArbFileApplyQC.setIsValid(1);
			tcArbFileApplyQC.setBusinessId(tcArbApplyDTO.getBusinessId());
			tcArbFileApplyQC.setIsMainCompact("1");
			List<TcArbFileApplyDTO> tcArbFileApplyDTOS = tcArbFileApiService.searchList(tcArbFileApplyQC);
			JSONObject jsonObject = new JSONObject();
			if (CollectionUtils.isNotEmpty(tcArbFileApplyDTOS)) {
				BaseLogUtils.newLogger(JSON.toJSONString(tcArbFileApplyDTOS)).setKeys("fadada").info();
				TcArbFileApplyDTO tcArbFileApplyDTO = tcArbFileApplyDTOS.get(0);
				String productId = tcArbFileApplyDTO.getProductId();
				String yuxin = PropKit.use("properties/fadada/fadada.properties").get("fadada.yunxin.cd");
				String wk = PropKit.use("properties/fadada/fadada.properties").get("fadada.wk.cd");

				if (productId.equals(yuxin)) {

					jsonObject.put("XD_YX_C2", "放款凭证");
					jsonObject.put("XD_YX_C30", "还款凭证");
					jsonObject.put("XD_YX_C13", "还款（逾期）情况说明");
					jsonObject.put("XD_YX_C19", "代偿凭证");
					jsonObject.put("XD_YX_C23", "最高额保证合同");
					jsonObject.put("XD_YX_C24", "代偿明细");
					jsonObject.put("XD_YX_C26", "代偿情况说明");
					jsonObject.put("XD_YX_C27", "还款提醒通知");
					jsonObject.put("XD_YX_C28", "宣布全部借款提前到期通知");

					jsonObject.put("XD_YX_C7", "二要素");
					jsonObject.put("XD_YX_C20", "放款记录");
					jsonObject.put("XD_YX_C4", "信托贷款合同（主合同）");
					//	jsonObject.put("XD_YX_C4SS","信托贷款合同（主合同）日志");
					jsonObject.put("XD_YX_C5", "贷款服务协议");
					//	jsonObject.put("XD_YX_C5SS","贷款服务协议日志");
					jsonObject.put("XD_YX_C11", "征信授权书");
					//jsonObject.put("XD_YX_C11SS","征信授权书日志");
					jsonObject.put("XD_YX_C22", "个人征信查询授权委托书");
					//jsonObject.put("XD_YX_C22SS","个人征信查询授权委托书");
					jsonObject.put("XD_YX_C12", "扣款授权书");
					//jsonObject.put("XD_YX_C12SS","扣款授权书日志");
					jsonObject.put("XD_YX_C11SS", "合同日志");

				} else if (productId.equals(wk)) {

					jsonObject.put("XD_C2", "放款凭证");
					jsonObject.put("XD_C13", "还款（逾期）情况说明");
					jsonObject.put("XD_C19", "代偿凭证");
					jsonObject.put("XD_C23", "最高额保证合同");
					jsonObject.put("XD_C24", "代偿明细");
					jsonObject.put("XD_C26", "代偿情况说明");
					jsonObject.put("XD_C27", "还款提醒通知");
					jsonObject.put("XD_C28", "宣布全部借款提前到期通知");


					jsonObject.put("XD_C7", "二要素");
					jsonObject.put("XD_C20", "放款记录");
					jsonObject.put("XD_C4", "信托贷款合同（主合同）");
					//	jsonObject.put("XD_C4SS","信托贷款合同（主合同）日志");
					jsonObject.put("XD_C5", "贷款服务协议");
					//	jsonObject.put("XD_C5SS","贷款服务协议日志");
					jsonObject.put("XD_C11", "征信授权书");
					//	jsonObject.put("XD_C11SS","征信授权书日志");
					jsonObject.put("XD_C22", "个人征信查询授权委托书");
					//	jsonObject.put("XD_C22SS","个人征信查询授权委托书日志");
					jsonObject.put("XD_C29", "个人信息采集及使用通用授权委托书");
					//	jsonObject.put("XD_C29SS","个人信息采集及使用通用授权委托书日志");


					jsonObject.put("XD_C29SS", "合同日志");


				}
			}
			DataResultVO dataResultVO = new DataResultVO(BaseConstant.IS_YESNO_YES, "请求成功!", jsonObject);
			return dataResultVO;
		}
	}

}

